"""数据库模型定义"""
from sqlalchemy import Column, String, Integer, Text, DateTime, Boolean, Float, JSON
from sqlalchemy.sql import func
from datetime import datetime

from app.database.connection import Base


class ExamQuestion(Base):
    """法考真题表"""
    __tablename__ = "exam_questions"
    
    id = Column(Integer, primary_key=True, index=True)
    question_id = Column(String(100), unique=True, index=True, nullable=False)
    content = Column(Text, nullable=False)  # 题目内容
    question_type = Column(String(50))  # 题目类型：single_choice, multiple_choice, case_study等
    options = Column(JSON)  # 选项列表（JSON格式）
    correct_answer = Column(Text)  # 正确答案
    explanation = Column(Text)  # 解析
    subject = Column(String(50))  # 科目：民法、刑法、民诉等
    category = Column(String(100))  # 分类
    difficulty = Column(String(20))  # 难度：easy, medium, hard, expert
    knowledge_points = Column(JSON)  # 知识点列表（JSON格式）
    year = Column(Integer)  # 年份（2019-2024）
    exam_type = Column(String(50))  # 考试类型：客观题、主观题
    source_url = Column(String(500))  # 来源URL
    source_site = Column(String(100))  # 来源网站
    
    # LegalBERT 解析结果
    parsed_content = Column(JSON)  # 解析后的结构化内容
    entities = Column(JSON)  # 识别的实体列表
    classification = Column(JSON)  # 分类结果
    
    # 元数据
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    is_verified = Column(Boolean, default=False)  # 是否已验证
    is_active = Column(Boolean, default=True)  # 是否激活


class SearchRecord(Base):
    """搜索记录表"""
    __tablename__ = "search_records"
    
    id = Column(Integer, primary_key=True, index=True)
    search_query = Column(String(500), nullable=False)  # 搜索关键词
    search_type = Column(String(50))  # 搜索类型：api, crawler, both
    year_range = Column(String(50))  # 年份范围，如"2019-2024"
    status = Column(String(50), default="pending")  # 状态：pending, running, completed, failed
    total_found = Column(Integer, default=0)  # 找到的题目总数
    total_saved = Column(Integer, default=0)  # 成功保存的题目数
    error_message = Column(Text)  # 错误信息
    
    # 时间戳
    started_at = Column(DateTime(timezone=True), server_default=func.now())
    completed_at = Column(DateTime(timezone=True))
    created_at = Column(DateTime(timezone=True), server_default=func.now())


class QuestionSource(Base):
    """题目来源表"""
    __tablename__ = "question_sources"
    
    id = Column(Integer, primary_key=True, index=True)
    source_name = Column(String(100), unique=True, nullable=False)  # 来源名称
    source_url = Column(String(500))  # 来源URL
    source_type = Column(String(50))  # 来源类型：official, training_institute, forum等
    is_active = Column(Boolean, default=True)  # 是否激活
    last_crawled_at = Column(DateTime(timezone=True))  # 最后爬取时间
    crawl_interval_hours = Column(Integer, default=24)  # 爬取间隔（小时）
    
    # 统计信息
    total_questions = Column(Integer, default=0)  # 总题目数
    last_success_count = Column(Integer, default=0)  # 上次成功爬取的题目数
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

